package mytest.ssw.www.api;

import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import mytest.ssw.www.mylibrary.Contents;
import mytest.ssw.www.mylibrary.DeptNode;
import mytest.ssw.www.mylibrary.Student;
import mytest.ssw.www.mylibrary.StudentInfo;
import mytest.ssw.www.mylibrary.TTTTT;
import mytest.ssw.www.mylibrary.UserInfo;
import mytest.ssw.www.mylibrary.getTheContent;
import mytest.ssw.www.mylibrary.sendToContent;

/**
 * Created by 73409 on 2015/11/7.
 */
public class ApiImpl implements Api {
    private final static String APP_KEY = "ANDROID_KCOUPON";
    private final static String TIME_OUT_EVENT = "连接超时";
    private final static String TIME_OUT_EVENT_MSG = "连接服务器失败";
    private HttpEngine httpEngine;
    public ApiImpl() {
        httpEngine = HttpEngine.getInstance();
    }

    @Override
    public ApiResponse<Void> sendSmsCode4Register(String phoneNum) {
        Map<String, String> paramMap = new HashMap<String, String>();
        paramMap.put("appKey", APP_KEY);
        paramMap.put("method", SEND_SMS_CODE);
        paramMap.put("phoneNum", phoneNum);
        Type type = new TypeToken<ApiResponse<Void>>() {
        }.getType();
        try {
            return httpEngine.postHandle(paramMap,getDeptNode, type);
        } catch (IOException e) {
            return new ApiResponse(TIME_OUT_EVENT, TIME_OUT_EVENT_MSG);
        }
    }

    @Override
    public ApiResponse<Void> registerByPhone(String phoneNum, String code, String password) {
        Map<String, String> paramMap = new HashMap<String, String>();
        paramMap.put("appKey", APP_KEY);
        paramMap.put("method", REGISTER);
        paramMap.put("phoneNum", phoneNum);
        paramMap.put("code", code);
        paramMap.put("password", password);
        Type type = new TypeToken<ApiResponse<List<TTTTT>>>() {
        }.getType();
        try {
            return httpEngine.postHandle(paramMap,getDeptNode, type);
        } catch (IOException e) {
            return new ApiResponse(TIME_OUT_EVENT, TIME_OUT_EVENT_MSG);
        }
    }

    //测试使用
    @Override
    public ApiResponse<Void> loginByApp(String loginName, String password) {
        String virtualurl="webLogin.do";
        Map<String, String> paramMap = new HashMap<String, String>();
        paramMap.put("account", loginName);
        paramMap.put("password", password);
    //    paramMap.put("virtualurl",LOGINURL);
        Type type = new TypeToken<ApiResponse<Void>>() {
        }.getType();
        try {
            return httpEngine.postHandle(paramMap,LOGINURL, type);
        } catch (IOException e) {
            return new ApiResponse(TIME_OUT_EVENT, TIME_OUT_EVENT_MSG);
        }
    }

    @Override
    public ApiResponse<List<UserInfo>> listNewCoupon(int currentPage, int pageSize) {
        Map<String, String> paramMap = new HashMap<String, String>();
        paramMap.put("appKey", APP_KEY);

        paramMap.put("currentPage", String.valueOf(currentPage));
        paramMap.put("pageSize", String.valueOf(pageSize));

        Type type = new TypeToken<ApiResponse<List<TTTTT>>>() {
        }.getType();
        try {
            return httpEngine.postHandle(paramMap,LOGINURL, type);
        } catch (IOException e) {
            return new ApiResponse(TIME_OUT_EVENT, TIME_OUT_EVENT_MSG);
        }
    }

    @Override
    public ApiResponse<List<Contents>> SearchInfo(HashMap<String,String> map) {
        Map<String, String> paramMap = new HashMap<String, String>();
        String url=searchInfo;
        //    paramMap.put("pageNo","1");
        paramMap.put("rows","15");
        paramMap.put("sortName","num");
        paramMap.put("sortOrder","asc");
        if(map.get("num")!=null)     paramMap.put("num",map.get("num") );
        if(map.get("account")!=null)     paramMap.put("account",map.get("account") );
        if(map.get("QQ")!=null)     paramMap.put("QQ",map.get("QQ") );
        if(map.get("birthplace")!=null)     paramMap.put("birthplace",map.get("birthplace") );
        if(map.get("dormitory")!=null)     paramMap.put("dormitory",map.get("dormitory") );
        if(map.get("homeAddress")!=null)     paramMap.put("homeAddress",map.get("homeAddress") );
        if(map.get("nation")!=null)     paramMap.put("nation",map.get("nation") );
        if(map.get("parentName")!=null)     paramMap.put("parentName",map.get("parentName") );
        if(map.get("parentPhone")!=null)     paramMap.put("parentPhone",map.get("parentPhone") );
        if(map.get("phoneNum")!=null)     paramMap.put("phoneNum",map.get("phoneNum") );
        String deptNo= map.get("departmentNum");
        String num=map.get("num");     if(num!=null) {if(num.length()<12){ url=searchStuInfo;}else{url=searchStuInfo;}}
        if(deptNo!=null )
        {
            paramMap.put("departmentNum",deptNo );

            if(deptNo.contains("BM_01_01") )
            {
                url=searchTeacherInfo;
            }
            else {
                url=searchStuInfo;
            }
        }

        Type type = new TypeToken<ApiResponse<List<Contents>>>() {
        }.getType();
            try {
                 return httpEngine.postHandle(paramMap,url, type);
        } catch (IOException e) {
            return new ApiResponse(TIME_OUT_EVENT, TIME_OUT_EVENT_MSG);
        }
    }
    @Override
    public ApiResponse<UserInfo> GetUserInfo(String account){
        Map<String, String> paramMap = new HashMap<String, String>();
        paramMap.put("account", account);
        Type type = new TypeToken<ApiResponse<UserInfo>>() {
        }.getType();
        try {
            return httpEngine.postHandle(paramMap,getUserInfo, type);
        } catch (IOException e) {
            return new ApiResponse(TIME_OUT_EVENT, TIME_OUT_EVENT_MSG);
        }

    }
    @Override
    public ApiResponse<StudentInfo> GetSomeoneInfo(String account){
        Map<String, String> paramMap = new HashMap<String, String>();
        paramMap.put("num", account);
        String URL;
        if(account.length()>5)        //教师工号长度小于5
        {
            URL=getStuInfo;
        }
        else  {
            URL=geTeacherInfo;
        }
        Type type = new TypeToken<ApiResponse<StudentInfo>>() {
        }.getType();
        try {
            return httpEngine.postHandle(paramMap,URL,type);

        } catch (IOException e) {
            return new ApiResponse(TIME_OUT_EVENT,TIME_OUT_EVENT_MSG);
        }

    }
    @Override
    public List<DeptNode> GetDeptNode(){
        Map<String, String> paramMap = new HashMap<String, String>();
        paramMap.put("num","1");
      //  paramMap.put("virtualurl", getDeptNode);
        Type type = new TypeToken<List<DeptNode>>() {
        }.getType();
        try {
            List<DeptNode> response=httpEngine.postHandle(paramMap,getDeptNode, type);
            Log.i("Api: ", response.get(1).getFullName());
            return response;
        } catch (IOException e) {
            return null;
        }

    }
    @Override
    public ApiResponse<Student> modifyStuInfo(StudentInfo stu){
        String url=modifyStuInfo;
        String info= new Gson().toJson(stu);

        Type type=new TypeToken<ApiResponse<Student>>(){}.getType();
        try {
            return httpEngine.postHandle("student="+info, url, type);

        }
     catch (IOException e) {
        return null;
    }
    }
    @Override
    public List<getTheContent> GetTheContent(String reciver){
        Map<String, String> paramMap = new HashMap<String, String>();
        paramMap.put("loginNum",reciver);
        //paramMap.put("virtualurl", getDeptNode);
        Type type = new TypeToken<List<getTheContent>>() {
        }.getType();
        try {
            List<getTheContent> response=httpEngine.postHandle(paramMap,PERSONALURL, type);
//            Log.i("Api: ", response.get(1).getNoticeContent());
            return response;
        } catch (IOException e) {
            return null;
        }

    }

    //发送通知
    @Override
    public ApiResponse<Void> sendPost(String contentA, String sendPersonNumA,String receiverNumsA) {
//        contentA="23123123";
//        sendPersonNumA = "201211010218";
//        receiverNumsA="201211010221";
        //receiverNumsP = receiverNumsEdi.getText().toString();
        String virtualurl="business/notice/sendAndroidNotice.do";
        Map<String, String> paramMap = new HashMap<String, String>();
        paramMap.put("content", contentA);
        paramMap.put("sendPersonNum", sendPersonNumA);
        paramMap.put("receiverNums",receiverNumsA);
        Type type = new TypeToken<ApiResponse<List<sendToContent>>>() {
        }.getType();
        try {
            return httpEngine.postHandle(paramMap,virtualurl, type);
        } catch (IOException e) {
            return new ApiResponse(TIME_OUT_EVENT, TIME_OUT_EVENT_MSG);
        }
    }
}
